#include <iostream>
#include <assert.h>

#include <tws_debugging.h>
#include <xqtinilayout.h>
#include <xinifile.h>
#include "xinfomatversiontypeparser.h"

const char INI_SECTION[] = "";
const char INI_KEY_MAJOR[] = "MAJOR";
const char INI_KEY_MINOR[] = "MINOR";
const char INI_KEY_RELEASE[] = "RELEASE";
const char INI_KEY_BUILD[] = "BUILD";
const char VERSION_INI_FILE[] = "/usr/infomat/VERSION";

XInfomatVersionTypeParser::XInfomatVersionTypeParser() :
    m_result(INFOMAT_RESULT_OK)
{
    memset (&m_versionType, 0x00, sizeof(m_versionType));
}

XInfomatVersionTypeParser::~XInfomatVersionTypeParser()
{
}

void XInfomatVersionTypeParser::getResource(QByteArray in_datagram)
{
    char* data = new char[in_datagram.size()];
    memcpy(data, in_datagram.data(), in_datagram.size());
    assert(INFOMAT_CONFIG_PROTOCOL == *data);

	STInfomatVersionType* tmpStVersionType = reinterpret_cast<STInfomatVersionType*> (data);

	memcpy (&m_versionType, tmpStVersionType, sizeof(m_versionType));
    m_versionType.head.result = INFOMAT_RESULT_OK; //initialized

    switch (m_versionType.head.command)
    {
        case INFOMAT_COMMAND_READ:
            read();
            break;

        case INFOMAT_COMMAND_WRITE:
            write();
            break;

        default:
            DEBUG("Version: no such command");
            m_result = INFOMAT_RESULT_ERR;
            break;
    }
	
	/* **** clean up ***** */
	if ( NULL != data )
	{
		delete  data;
		data = NULL;
	}
	
    return;
}

void XInfomatVersionTypeParser::read()
{
    XIniFile iniFile;

    assert(0 == iniFile.open(VERSION_INI_FILE));

    int major;
    if (0 != iniFile.readInt(INI_SECTION, INI_KEY_MAJOR, major))
        m_result = INFOMAT_RESULT_ERR;
    m_versionType.major = (uint8_t)(major);

    int minor;
    if (0 != iniFile.readInt(INI_SECTION, INI_KEY_MINOR, minor))
        m_result = INFOMAT_RESULT_ERR;
    m_versionType.minor = (uint8_t)(minor);

    int release;
    if (0 != iniFile.readInt(INI_SECTION, INI_KEY_RELEASE, release))
        m_result = INFOMAT_RESULT_ERR;
    m_versionType.release = (uint8_t)(release);

    int build;
    if (0 != iniFile.readInt(INI_SECTION, INI_KEY_BUILD, build))
        m_result = INFOMAT_RESULT_ERR;
    m_versionType.build = (uint8_t)(build);

    iniFile.close();

    return;
}

void XInfomatVersionTypeParser::write()
{
    XIniFile iniFile;
    assert(0 == iniFile.open(VERSION_INI_FILE, EIniReadWrite));

    if (0 != iniFile.writeInt(INI_SECTION, INI_KEY_MAJOR, int(m_versionType.major)))
        m_result = INFOMAT_RESULT_ERR;

    if (0 != iniFile.writeInt(INI_SECTION, INI_KEY_MINOR, int(m_versionType.minor)))
        m_result = INFOMAT_RESULT_ERR;

    if (0 != iniFile.writeInt(INI_SECTION, INI_KEY_RELEASE, int(m_versionType.release)))
        m_result = INFOMAT_RESULT_ERR;

    if (0 != iniFile.writeInt(INI_SECTION, INI_KEY_BUILD, int(m_versionType.build)))
        m_result = INFOMAT_RESULT_ERR;

	iniFile.close();
    iniFile.flush();
    return;
}

QByteArray XInfomatVersionTypeParser::getOutDatagram()
{
    m_versionType.head.result = m_result;
	QByteArray outdata = QByteArray(reinterpret_cast<char*> (&m_versionType), sizeof(m_versionType));
#ifdef _DEBUGING_VERSION_
	for (int i=0; i<outdata.size(); ++i)
		DEBUG("byte:%02d - %02d", i, *(outdata.data()+ i));
#endif
	return outdata;
}

